package com.tangyunlong.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangyunlong.mall.entity.Manager;
import com.tangyunlong.mall.mapper.ManagerMapper;
import com.tangyunlong.mall.service.MangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements MangerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public void add(com.tangyunlong.mall.entity.Manager manager) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("account", manager.getAccount());
        boolean exists = managerMapper.exists(wrapper);
        if (exists){
            throw new ArithmeticException("账号"+manager.getAccount()+"已存在");
        }else {
            manager.setStatus("y");
            managerMapper.insert(manager);
        }
    }
    @Override
    public void update(com.tangyunlong.mall.entity.Manager manager) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("account", manager.getAccount());
        boolean exists = managerMapper.exists(wrapper);
        if (exists){
            throw new ArithmeticException("账号"+manager.getAccount()+"已存在");
        }else {
            manager.setStatus("y");
            managerMapper.updateById(manager);
        }
    }

    @Override
    public Manager login(String account, String password) {
        LambdaQueryWrapper<Manager> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Manager::getAccount,account);
        wrapper.eq(Manager::getPassword,password);
        return managerMapper.selectOne(wrapper);
    }
}
